package com.ruoyi.web.controller.clothing;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.clothing.domain.User;
import com.ruoyi.clothing.domain.vo.AddBuild;
import com.ruoyi.clothing.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.clothing.domain.SysBuild;
import com.ruoyi.clothing.service.ISysBuildService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 身材管理Controller
 *
 * @author weh
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/clothing/build")
public class SysBuildController extends BaseController
{
    @Autowired
    private ISysBuildService sysBuildService;

    @Autowired
    private IUserService userService;

    /**
     * 查询身材管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SysBuild sysBuild)
    {
        startPage();
        List<SysBuild> list = sysBuildService.selectSysBuildList(sysBuild);
        return getDataTable(list);
    }

    /**
     * 导出身材管理列表
     */

    @Log(title = "身材管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBuild sysBuild)
    {
        List<SysBuild> list = sysBuildService.selectSysBuildList(sysBuild);
        ExcelUtil<SysBuild> util = new ExcelUtil<SysBuild>(SysBuild.class);
        util.exportExcel(response, list, "身材管理数据");
    }

    /**
     * 获取身材管理详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sysBuildService.selectSysBuildById(id));
    }

    /**
     * 新增身材管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:build:add')")
    @Log(title = "身材管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBuild sysBuild)
    {
        return toAjax(sysBuildService.insertSysBuild(sysBuild));
    }

    /**
     * 修改身材管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:build:edit')")
    @Log(title = "身材管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBuild sysBuild)
    {
        return toAjax(sysBuildService.updateSysBuild(sysBuild));
    }

    /**
     * 删除身材管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:build:remove')")
    @Log(title = "身材管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysBuildService.deleteSysBuildByIds(ids));
    }

    /**
     * 根据用户名查询出对应的身材列表
     * @param username 用户名
     * @return 身材列表
     */
    @GetMapping("/selectBuildListByUsername")
    public List<SysBuild> selectBuildListByUsername(@RequestParam String username){
        User user = userService.selectUserByUserName(username);
        Long id = user.getId();
        return sysBuildService.selectBuildListById(id);
    }

    /**
     * 新增身材数据
     * @param data 身材数据
     * @return 结果
     */
    @PostMapping("/insertBuild")
    public AjaxResult insertBuild(@RequestBody AddBuild data){
        sysBuildService.insertBuild(data);
        return AjaxResult.success("插入成功");
    }

    /**
     * 根据id删除身材数据
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBuild")
    public AjaxResult deleteBuild(@RequestParam Integer id){
        sysBuildService.deleteBuild(id);
        return AjaxResult.success("删除成功");
    }

}
